<?php
App::uses('AppController', 'Controller');
/**
 * Userlogs Controller
 *
 * @property Userlog $Userlog
 */
class UserlogsController extends AppController {

	public $SideBarItems = array();
	public $SideBarMessage = '';
	public $HMenu = '/';
	public $VMenu = '';

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Userlog->recursive = 0;
		$this->set('Userlogs', $this->paginate());
		// получаем список пользователей online
		$this->loadModel('Onlineuser');
		$onlineusers = $this->Onlineuser->find('all');
		$this->set('onlineusers', $onlineusers);
		
		$this->SideBarItems = array(
				'Сброс логов админа' => array('_self'  => '/userlogs/clearcustomUserlog'),
				'Очистить журнал логов' => array('_self'  => '/userlogs/clearUserlog'),
				'Очистить список online' => array('_self'  => '/userlogs/clearonline'),
		);
		
		$this->set('SideBarItems', $this->SideBarItems);
		$this->set('SideBarMessage', $this->SideBarMessage);
		
		$this->layout = 'defaultwsb';
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Userlog->id = $id;
		if (!$this->Userlog->exists()) {
			throw new NotFoundException(__('Invalid Userlog'));
		}
		$this->set('Userlog', $this->Userlog->read(null, $id));
	}

	public function clearUserlog(){
		$this->Userlog->deleteall(array('Userlog.id !=' => null));
		$this->Session->setFlash(__('Журнал логов был полностью очищен.'));
		$this->redirect(array('action' => 'index'));
		
	}
	public function clearcustomUserlog() {
		$this->Userlog->deleteall(array('Userlog.login' => 'admin'));
		$this->Session->setFlash(__('Журнал логов админа был очищен.'));
		$this->redirect(array('action' => 'index'));
	}
	
	public function clearonline() {
		$this->loadModel('Onlineuser');
		$this->Onlineuser->deleteall(array('Onlineuser.id !=' => null));
		$this->Session->setFlash(__('Журнал пользователей online был полностью очищен.'));
		$this->redirect(array('action' => 'index'));		
	}
/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Userlog->create();
			if ($this->Userlog->save($this->request->data)) {
				$this->Session->setFlash(__('The Userlog has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Userlog could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Userlog->id = $id;
		if (!$this->Userlog->exists()) {
			throw new NotFoundException(__('Invalid Userlog'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Userlog->save($this->request->data)) {
				$this->Session->setFlash(__('The Userlog has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Userlog could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Userlog->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Userlog->id = $id;
		if (!$this->Userlog->exists()) {
			throw new NotFoundException(__('Invalid Userlog'));
		}
		if ($this->Userlog->delete()) {
			$this->Session->setFlash(__('Userlog deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Userlog was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
